'use strict'

import axios from 'axios'
import qs from 'qs'

axios.interceptors.request.use(config => {
  // loading
  config.data = JSON.stringify(config.data);
  config.headers={
    'Content-Type':'application/x-www-form-urlencoded'
  }
  return config
}, error => {
  return Promise.reject(error)
})

axios.interceptors.response.use(response => {
  return response
}, error => {
  return Promise.resolve(error.response)
})
function isEmptyObject(e) {  
    var t;  
    for (t in e)  
        return false;  //返回false,表示不是空对象
    return true //返回true，表示是空对象 
}  
const axiosMethod = {
  post:(url, data,dataType)=> {
   if(dataType=="jsonp"){
      return new Promise(function(resolve, reject){
          window.axiosMethodResponse = (response)=>{
              resolve(response);
              document.body.removeChild(script);
          }
          var script = document.createElement("script");
          var argumentString = "";
          if(isEmptyObject(data)){
              argumentString = "?callback=axiosMethodResponse"
          }else{
            data = JSON.stringify(data);
            data = data.replace(/\{/g,"").replace(/\}/g,"").replace(/\:/g,"=").replace(/\"/g,"").replace(/,/g,"&");
            argumentString = "?"+data+"&callback=axiosMethodResponse"
          }
          script.src =url+argumentString;
          var theScriptId = 'jsonpScript'+new Date().getTime();
          document.body.appendChild(script, document.body.firstChild);
          script.onerror = function(){
            reject("网络异常");
          }
      }).then(function(response){
          return response;
      }).then(function(msg){
          return msg;
      });
    }else{
        return axios({
                method: 'post',
                url,
                data: qs.stringify(data),
                timeout: 10000,
                headers: {
                  'X-Requested-With': 'XMLHttpRequest',
                  'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                }
              }).then(
                (response) => {
                  return response.data
                }
              ).then(
                (res) => {
                  return res
                }
              )
    }
    
    
  },
  get:(url, params)=> {
    if(dataType=="jsonp"){
      return new Promise(function(resolve, reject){
          window.axiosMethodResponse = (response)=>{
              resolve(response);
              document.body.removeChild(script);
          }
          var script = document.createElement("script");
          var argumentString = "";
          if(isEmptyObject(data)){
              argumentString = "?callback=axiosMethodResponse"
          }else{
            data = JSON.stringify(data);
            data = data.replace(/\{/g,"").replace(/\}/g,"").replace(/\:/g,"=").replace(/\"/g,"").replace(/,/g,"&");
            argumentString = "?"+data+"&callback=axiosMethodResponse"
          }
          script.src =url+argumentString;
          var theScriptId = 'jsonpScript'+new Date().getTime();
          script.id = theScriptId
          document.body.appendChild(script, document.body.firstChild);
          script.onerror = function(){
            reject("网络异常");
          }
      }).then(function(response){
          return response;
      }).then(function(msg){
          return msg;
      });
    }else{
      return axios({
        method: 'get',
        url:url,
        params, // get 请求时带的参数
        timeout: 10000,
        headers: {
          'X-Requested-With': 'XMLHttpRequest'
        }
      }).then(
        (response) => {
          return response.data
        }
      ).then(
        (res) => {
          return res
        }
      )}
  }
}
export default axiosMethod